package com.dc.controller;

import com.dc.base.aop.AopOperation;
import com.dc.base.controller.BaseController;
import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.em.RoleMenuEnum;
import com.dc.base.utils.RSAUtils;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.model.User;
import com.dc.service.UserService;
import com.wordnik.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

@Api("用户模块")
@RequestMapping("/user")
@Controller
public class UserController extends BaseController {
    @Autowired
    UserService userService;
    @ApiOperation(value="第一个springmvc的接口",httpMethod = "GET",notes = "查询")
    @RequestMapping(value="helloWord", method = RequestMethod.GET)
    @ResponseBody
    public String helloWord(){
        return "helloWord";
    }
    @ApiOperation(value="分页查询用户信息",httpMethod = "GET",notes = "查询")
    @RequestMapping(value = "/selectPageUser",method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码",name = "curr_page",defaultValue = "1",dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "int",paramType = "query")
    })
    public BaseModel selectPageUser(int curr_page, int page_size, BaseModel baseModel){
        return userService.selectPageUser(curr_page,page_size,baseModel);

    }
    //添加一个登录接口,账号必须为手机号格式，
    // 密码必须超过六位，校验通过提示登录成功，否则提示相应错误消息
    @ApiOperation(value="登录",httpMethod = "POST",notes = "查询")
    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "账号",name = "account",required = true,dataType = "string",paramType = "query"),
//            @ApiImplicitParam(value = "密码",name = "password",required = true,dataType = "string",paramType = "query")
//    })
    public BaseModel selectPageUser(@RequestParam(value = "account",required = true) String account,
                                    @RequestParam(value = "pwd",required = true)String pwd, BaseModel baseModel,
                                    HttpServletRequest request
        )throws Exception{
        //抛出系统异常
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
        if (!Pattern.matches(regex, account) || pwd.length()<6) {
            throw new BusinessException(ErrorMessageEnum.FORMAT_ERROR);
        }
        HttpSession session=request.getSession();
        // pwd是一个密文，需要先解密出明文再比对
        String whitePwd=RSAUtils.sessionDecryptHexStr(session,pwd);
        //将明文转换成md5后的密文
        String md5Pwd= com.dc.base.util.MD5DES.encrypt(whitePwd);
        // 调用业务层
        baseModel=userService.login(account,md5Pwd,baseModel);
        // 将登录用户的数据存入到session
        session.setAttribute("currentUser",baseModel.getData());
        return baseModel;
    }
    //新增接口，要求所有的错误都有统一异常处理机制处理
    @AopOperation(desc = "新增用户操作",type = "新增",menu = RoleMenuEnum.NO_204)
    @ApiOperation(value="用户新增",httpMethod = "POST",notes = "新增")
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号",name="account",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "姓名",name="name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "头像",name="head_img_url",dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "籍贯",name="address",dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "邮箱",name="email",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "手机号",name="phone",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "性别",name="sex",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "描述",name="memo",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "生日",name="birthdayStr",required = true,dataType = "String",paramType = "query"),
    })
    public BaseModel addUser(User user,String birthdayStr,HttpServletRequest request,BaseModel baseModel)throws Exception{
        //前端数据校验
        user.setPassword(com.dc.base.util.MD5DES.encrypt("111111"));
        user.setState(1);
        user.setIs_deleted(0);
        user.setCreate_time(new Date());
        user.setVersion(0);
        // 生日  字符串转事件
        if (birthdayStr!=null&&!birthdayStr.equals("")){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=simpleDateFormat.parse(birthdayStr);
            user.setBirthday(date);
        }
        HttpSession session=request.getSession();
        User currUser=(User)session.getAttribute("currentUser");
        if(currUser == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        user.setCreate_user_id(currUser.getUser_id());
        user.setCreate_user_name(currUser.getName());
        return userService.addUser(user,baseModel);
    }
    @ApiOperation(value="用户修改",httpMethod = "POST",notes = "修改")
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "账号",name="account",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "姓名",name="name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "头像",name="head_img_url",dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "籍贯",name="address",dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "邮箱",name="email",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "手机号",name="phone",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "性别",name="sex",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "描述",name="memo",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "生日",name="birthdayStr",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "用户编号",name="user_id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "版本号",name="version",required = true,dataType = "String",paramType = "query"),
    })
    public BaseModel updateUser(User user,String birthdayStr,BaseModel baseModel,HttpServletRequest request)throws Exception{
        //校验格式
        // 获取登录信息
        HttpSession session=request.getSession();
        User currentUser=(User)session.getAttribute("currentUser");
        // 时间字符串，转时间格式
        if (birthdayStr!=null&&!birthdayStr.equals("")){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=simpleDateFormat.parse(birthdayStr);
            user.setBirthday(date);
        }
        if(currentUser==null){
            throw  new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        user.setUpdate_user_id(currentUser.getUser_id());
        user.setUpdate_time(new Date());
        user.setUpdate_user_name(currentUser.getName());
        return userService.updateUser(user,baseModel);
    }
    @ApiOperation(value="用户删除",httpMethod = "POST",notes = "删除")
    @RequestMapping(value = "/deleteUser/{ids}",method = RequestMethod.POST)
    @ResponseBody
    public BaseModel deleteUser(@PathVariable(value = "ids") String ids,BaseModel baseModel,HttpServletRequest request)throws  Exception{
        HttpSession session=request.getSession();
        User currentUser=(User)session.getAttribute("currentUser");
        if(currentUser==null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return userService.deleteUser(ids,baseModel);
    }
}
